﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddSeason : System.Web.UI.Page
{
    ManageDatabase data;

    Season season;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (!IsPostBack)
            {
                GetSeries();
                Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
            }
            season = new Season();
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    private void GetSeries()
    {
        ListItemCollection allSeries = data.GetSeriesSimple();
        AddSeasonChooseSeries.Items.Clear();
        AddSeasonChooseSeries.Items.Add(new ListItem("--Válasszon sorozatot--", "0"));
        foreach (ListItem item in allSeries)
        {
            AddSeasonChooseSeries.Items.Add(item);
        } 
    }

    protected void SeasonCreate_Click(object sender, EventArgs e)
    {
        try{
            
            if(AddSeasonChooseSeries.SelectedValue == "0"){
                Alert.Show("Kérem válasszon ki egy sorozatot!");
                return;
            }
            else{
                season.seriesId = Convert.ToInt32(AddSeasonChooseSeries.SelectedValue);
            }
            try{
                season.season = Convert.ToInt32(AddSeasonNumber.Text);
            }catch(Exception){
                Alert.Show("Az évad száma csak numerikus lehet!");
                return;
            }
        
            if (AddSeasonReleaseDate.Text != "")
            {
                try
                {
                    season.releaseDate = DateTime.Parse(AddSeasonReleaseDate.Text);
                }
                catch (Exception)
                {
                    Alert.Show("Érvénytelen a megjelenés dátuma!");
                    return;
                }
            }

            if (AddSeasonEmbedVideo.Text.IndexOf("&") == -1)
            {
                season.videoUrl = AddSeasonEmbedVideo.Text.Substring((AddSeasonEmbedVideo.Text.IndexOf("v=") + 2), (AddSeasonEmbedVideo.Text.Length - AddSeasonEmbedVideo.Text.IndexOf("v=") - 2));
            }
            else
            {
                season.videoUrl = AddSeasonEmbedVideo.Text.Substring((AddSeasonEmbedVideo.Text.IndexOf("v=") + 2), (AddSeasonEmbedVideo.Text.IndexOf("&") - AddSeasonEmbedVideo.Text.IndexOf("v=") - 2));
            }
            season.description = AddSeasonDescription.Text;

            season.imgUrl = "";
            if (AddSeasonImg.HasFile)
            {
                try
                {
                    if (AddSeasonImg.PostedFile.ContentType == "image/pjpeg" || AddSeasonImg.PostedFile.ContentType == "image/x-jpeg") //image/gif,image/x-png
                    {
                        if (AddSeasonImg.PostedFile.ContentLength < 512000)
                        {
                            string filename = "season_" + String.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".jpeg";
                            AddSeasonImg.SaveAs(Server.MapPath("~/Pic/") + filename);
                            season.imgUrl = filename;
                        }
                        else
                        {
                            StatusLabel.Text = "A maximális képméret 500 kb!";
                            return;
                        }
                    }
                    else
                    {
                        StatusLabel.Text = "Az alábbi képformátum nem támogatott: " + AddSeasonImg.PostedFile.ContentType;
                        return;
                    }
                }
                catch (Exception)
                {
                    StatusLabel.Text = "Hiba történt a kép feltöltése során.";
                    return;
                }
            }

            Boolean success = data.InsertSeason(season, Functions.GetUserId(), User.IsInRole("admin"));

            if (!success)
            {
                Alert.Show("Hiba történt az éved létrehozása során!");
            }
            else
            {
                Response.Redirect("Series.aspx");
            }

        }catch(Exception){
            Alert.Show("Hiba történt az éved létrehozása során!");
        }
    }
    protected void SeasonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Series.aspx");
    }
    
    
}
